<?php
namespace app\index\controller;
use \think\Controller;
use \think\View;
use app\index\model\TodayInHistory;
use app\index\model\Weather;
class Chat extends Controller
{
	public function index()
	{
		return $this->fetch('index');
	}
	public function return_msg()
	{
		if(!isset($_REQUEST['msg'])){
			echo json_encode(array('status'=>1,'msg'=>'请输入内容'));exit;
		}
		$msg = $_REQUEST['msg'];
		if($msg=="历史上的今天"){
			Chat::todayInHistory();
		}
		$url = "http://api.qingyunke.com/api.php?key=free&appid=0&msg='".$msg."'";
        $return_data = file_get_contents($url);
        //$data = json_decode($return_data);
        //echo $data['content'];exit;
        $explode_data = explode(',"content":"', $return_data);
       $data = substr($explode_data[1],0,strlen($explode_data[1])-2);
        echo json_encode(array('status'=>1,'msg'=>$data));exit;
	}
	//历史上的今天
     public static function todayInHistory()
    {
        $todayInHistory = new TodayInHistory();
        $res = $todayInHistory->todayInHistory();
        //var_dump($res);
        $data = "";
        for ($i=0; $i < count($res) ; $i++) { 
          $data = $data.$res[$i]->year.'-'.$res[$i]->month.'-'.$res[$i]->day.'：'.$res[$i]->title.'<br>';
        }
        echo json_encode(array('status'=>1,'msg'=>$data));exit;
    }
}